﻿using System;

namespace ConsoleApp1
{
    class Program
    {
        //1:Func 委托
        //private delegate string Say();

        //public static string SayHello()
        //{
        //    return "Hello";
        //}
        //static void Main(string[] args)
        //{
        //    //1.Func<string> say = SayHello;
        //    //2.Say say = SayHello;
        //    Console.WriteLine(say());
        //}


        //2:Action 委托
        //static void Main(string[] args)
        //{
        //    Action say = SayHello;
        //    say();
        //}
        //public static void SayHello()
        //{
        //    Console.WriteLine("Say Hello");
        //}

        //3:Action<T> 委托：传入参数为T，没有返回类型。
        //static void Main(string[] args)
        //{
        //    Action<string> say = SayHello;
        //    say("Hello");
        //}
        //public static void SayHello(string word)
        //{
        //    Console.WriteLine(word);
        //}

        //4:Action接匿名方法
        //static void Main(string[] args)
        //{
        //    Action<string> say = delegate (string word)
        //    {
        //        Console.WriteLine(word);
        //    };
        //    say("Hello Word");
        //}

        //5:Action接Lambda表达式
        //static void Main(string[] args)
        //{
        //    Action<string> say = s => Console.WriteLine(s);
        //    say("Hello Word");
        //}

        //6:Func<T, TResult> 委托：有一个传入参数T，
        //委托 传入参数类型为string,方法返回类型为int,
        //static void Main(string[] args)
        //{
        //    Func<string, int> a = Count;
        //    var test = a("5");
        //    Console.WriteLine(test);
        //}
        ////对应方法
        //public static int Count(string num)
        //{
        //    return Convert.ToInt32(num);
        //}


        //7:
        //static void Main(string[] args)
        //{
        //    Func<int, int, int> func = Add;
        //    var abc = func(100, 200);
        //    Console.WriteLine(abc);
        //}
        //private static int Add(int num1, int num2)
        //{
        //    return num1 + num2;
        //}

        //8:Func与匿名方法一起使用
        //public static void Main()
        //{
        //    Func<string, int, string[]> extractMeth = delegate (string s, int i)
        //    {
        //        char[] delimiters = new char[] { ' ' };
        //        return i > 0 ? s.Split(delimiters, i) : s.Split(delimiters);
        //    };

        //    string title = "The Scarlet Letter";
        //    // Use Func instance to call ExtractWords method and display result
        //    foreach (string word in extractMeth(title, 5))
        //        Console.WriteLine(word);
        //}

        //9:Func接 lambda 表达式
        public static void Main()
        {
            char[] separators = new char[] { ' ' };
            Func<string, int, string[]> extract = (s, i) =>
                 i > 0 ? s.Split(separators, i) : s.Split(separators);

            string title = "The Scarlet Letter";
            // Use Func instance to call ExtractWords method and display result
            foreach (string word in extract(title, 5))
                Console.WriteLine(word);
        }
    }
}
